package com.example.administrator.myapplication;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.example.administrator.myapplication.base.BaseActivity;
import com.example.administrator.myapplication.media.Music;
import com.example.administrator.myapplication.media.Sound;

/**
 * Created by Administrator on 2016/3/9 0009.
 */
public class MenuActivity extends BaseActivity implements View.OnClickListener{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);

        findViewById(R.id.imageButton1).setOnClickListener(this);
        findViewById(R.id.imageButton2).setOnClickListener(this);
        findViewById(R.id.imageButton3).setOnClickListener(this);
        findViewById(R.id.imageButton4).setOnClickListener(this);
        findViewById(R.id.imageButton5).setOnClickListener(this);
        findViewById(R.id.imageButton6).setOnClickListener(this);
        //开始动画
        rotate();
        //播放音乐
        Music.init(this);
        Music.load(this);
        Music.startMusic(this,true,R.raw.play);

        //初始化音效池
        Sound.init(this,new int[]{R.raw.sound1,R.raw.sound2,R.raw.sound3});


    }
    //监听返回按键
    @Override
    public void onBackPressed() {

        exitGame();
    }

    @Override
    public void onClick(View v) {
        Sound.play(0);
        switch (v.getId()){
            case R.id.imageButton1:
                startGame();
                break;
            case R.id.imageButton2:
                rankGame();
                break;
            case R.id.imageButton3:
                settingGame();
                break;
            case R.id.imageButton4:
                helpGame();
                break;
            case R.id.imageButton5:
                aboutGame();
                break;
            case R.id.imageButton6:

                exitGame();
                break;
        }
    }
    //关于游戏
    private void aboutGame(){
        AlertDialog.Builder builder =  new AlertDialog.Builder(this);
        builder.setIcon(R.drawable.about).setTitle("关于游戏").setMessage("版权所有！谢谢试玩！");
        builder.setNeutralButton("返回", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Sound.play(1);
            }
        });
        builder.show();
    }
    //退出游戏
    private void exitGame(){
        AlertDialog.Builder builder =  new AlertDialog.Builder(this);
        builder.setTitle("退出游戏").setMessage("确定要退出游戏吗？").setIcon(R.drawable.about);
        builder.setNegativeButton("返回", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Sound.play(1);
            }
        });
        builder.setPositiveButton("退出", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Sound.play(1);
                finish();
            }
        });
        builder.show();
    }
    //开始游戏
    private void startGame(){
        startActivity(SelectActivity.class);
    }
    //游戏帮助
    private void helpGame(){
        startActivity(HelpActivity.class);

    }
    //游戏设置
    private void settingGame(){
        startActivity(SettingActivity.class);
    }
    //积分排名
    private void rankGame(){
        startActivity(RankActivity.class);
    }
    //设置图片旋转
    private void rotate(){
        Animation animation = AnimationUtils.loadAnimation(this,R.anim.anim_rotate);
        findViewById(R.id.imageButton1).startAnimation(animation);
        findViewById(R.id.imageButton2).startAnimation(animation);
        findViewById(R.id.imageButton3).startAnimation(animation);
        findViewById(R.id.imageButton4).startAnimation(animation);
        findViewById(R.id.imageButton5).startAnimation(animation);
        findViewById(R.id.imageButton6).startAnimation(animation);
    }
    //保存用户偏好设置

    @Override
    protected void onDestroy() {
        Music.save(this);
        Music.stopMusic();
        super.onDestroy();
    }
}
